
import site.assad.groovy.DbUserDao
import site.assad.groovy.XmlUserDao
import site.assad.groovy.LogDao
import site.assad.groovy.LogonService

import org.springframework.core.io.ClassPathResource
/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/11/30 21:21
 * Description: Groovy DSL Bean 配置脚本, 演示 Groovy DSL 配置的灵活性
 */

beans {
    //声明 context 命名空间
    xmlns context: "http://www.springframework.org/schema/context"

    //与注解混合使用，定义注解Bean的扫描包
    context.'component-scan'('base-package': "com.smart.groovy") {
        'exclude-filter'('type': "aspectj", 'expression': "com.smart.xml.*")  //排除不需要的包
    }

    //读取 app-conf.properties 配置文件
    def stream
    def config = new Properties()
    try{
        stream = new ClassPathResource('conf/app-conf.properties').inputStream
        config.load(stream)
    }finally{
        if(stream != null)
            stream.close()
    }

    //根据条件注入Bean
    if(config.get('dataProvider') == 'db'){
        userDao(DbUserDao)
    }else{
        userDao(XmlUserDao)
    }

    //配置无参构造函数注入Bean
    logDao(LogDao){
        bean ->
            bean.scope = "prototype"
            bean.initMethod="init"
            bean.destroyMethod="destory"
            bean.lazyInit = true
    }

    //配置由参数构造函数注入Bean，对应 LogonService 以 UserDao作为参数的构造函数
    logonService(LogonService,userDao){
        logDao = ref('logDao')   //配置属性注入，引用Groovy定义的Bean
    }


}